import {
  Body,
  Controller,
  Delete,
  Get,
  HttpCode,
  HttpStatus,
  NotFoundException,
  Param,
  Patch,
  Post,
  Put,
  ValidationPipe,
} from '@nestjs/common';
import { ApiOperation, ApiParam, ApiResponse, ApiTags } from '@nestjs/swagger';
import { PaymentMethodConfig } from '../config/payment-methods.config';
import { Payment } from '../entities/payment.entity';
import { CreatePaymentDto, UpdatePaymentDto } from './dto';
import { PaymentsService } from './payments.service';

@ApiTags('payments')
@Controller('payments')
export class PaymentsController {
  constructor(private readonly paymentsService: PaymentsService) {}

  @Get('methods')
  @ApiOperation({
    summary: '获取可用支付方式',
    description: '返回所有可用的支付方式',
  })
  @ApiResponse({
    status: 200,
    description: '成功获取支付方式列表',
    type: [PaymentMethodConfig],
  })
  getAvailablePaymentMethods(): PaymentMethodConfig[] {
    return this.paymentsService.getAvailablePaymentMethods();
  }

  @Get()
  @ApiOperation({
    summary: '获取所有支付记录',
    description: '返回所有支付记录的列表',
  })
  @ApiResponse({
    status: 200,
    description: '成功获取支付记录列表',
    type: [Payment],
  })
  async findAll(): Promise<Payment[]> {
    return this.paymentsService.findAll();
  }

  @Get('order/:orderId')
  @ApiOperation({
    summary: '获取订单的所有支付记录',
    description: '根据订单ID获取其所有支付记录',
  })
  @ApiParam({ name: 'orderId', description: '订单ID' })
  @ApiResponse({
    status: 200,
    description: '成功获取支付记录列表',
    type: [Payment],
  })
  @ApiResponse({ status: 404, description: '订单不存在' })
  async findByOrder(@Param('orderId') orderId: string): Promise<Payment[]> {
    const id = parseInt(orderId, 10);
    if (isNaN(id)) {
      throw new NotFoundException('订单ID必须是数字');
    }
    return this.paymentsService.findByOrder(id);
  }

  @Get(':id')
  @ApiOperation({
    summary: '获取指定支付记录',
    description: '根据ID获取支付记录详情',
  })
  @ApiParam({ name: 'id', description: '支付记录ID' })
  @ApiResponse({
    status: 200,
    description: '成功获取支付记录详情',
    type: Payment,
  })
  @ApiResponse({ status: 404, description: '支付记录不存在' })
  async findOne(@Param('id') id: string): Promise<Payment> {
    const paymentId = parseInt(id, 10);
    if (isNaN(paymentId)) {
      throw new NotFoundException('支付记录ID必须是数字');
    }
    return this.paymentsService.findOne(paymentId);
  }

  @Post()
  @ApiOperation({ summary: '创建支付记录', description: '创建新的支付记录' })
  @ApiResponse({ status: 201, description: '成功创建支付记录', type: Payment })
  @ApiResponse({
    status: 400,
    description: '无效的请求数据或支付方式当前不可用',
  })
  @ApiResponse({ status: 404, description: '订单不存在' })
  async create(
    @Body(ValidationPipe) createPaymentDto: CreatePaymentDto,
  ): Promise<Payment> {
    return this.paymentsService.create(createPaymentDto);
  }

  @Put(':id')
  @ApiOperation({
    summary: '更新支付记录',
    description: '更新指定ID的支付记录信息',
  })
  @ApiParam({ name: 'id', description: '支付记录ID' })
  @ApiResponse({ status: 200, description: '成功更新支付记录', type: Payment })
  @ApiResponse({
    status: 400,
    description: '无效的请求数据或支付方式当前不可用',
  })
  @ApiResponse({ status: 404, description: '支付记录不存在' })
  async update(
    @Param('id') id: string,
    @Body(ValidationPipe) updatePaymentDto: UpdatePaymentDto,
  ): Promise<Payment> {
    const paymentId = parseInt(id, 10);
    if (isNaN(paymentId)) {
      throw new NotFoundException('支付记录ID必须是数字');
    }
    return this.paymentsService.update(paymentId, updatePaymentDto);
  }

  @Patch(':id/mark-paid')
  @ApiOperation({
    summary: '标记支付完成',
    description: '将支付记录标记为已支付',
  })
  @ApiParam({ name: 'id', description: '支付记录ID' })
  @ApiResponse({
    status: 200,
    description: '成功标记支付记录为已支付',
    type: Payment,
  })
  @ApiResponse({ status: 404, description: '支付记录不存在' })
  async markAsPaid(@Param('id') id: string): Promise<Payment> {
    const paymentId = parseInt(id, 10);
    if (isNaN(paymentId)) {
      throw new NotFoundException('支付记录ID必须是数字');
    }
    return this.paymentsService.markAsPaid(paymentId);
  }

  @Delete(':id')
  @ApiOperation({
    summary: '删除支付记录',
    description: '删除指定ID的支付记录',
  })
  @ApiParam({ name: 'id', description: '支付记录ID' })
  @ApiResponse({ status: 204, description: '成功删除支付记录' })
  @ApiResponse({ status: 404, description: '支付记录不存在' })
  @HttpCode(HttpStatus.NO_CONTENT)
  async remove(@Param('id') id: string): Promise<void> {
    const paymentId = parseInt(id, 10);
    if (isNaN(paymentId)) {
      throw new NotFoundException('支付记录ID必须是数字');
    }
    return this.paymentsService.remove(paymentId);
  }
}
